﻿#include "park.h"
#include <time.h>
enum {
	year,
	month,
	day,
	hour,
	minute,
	enum_size
};
int day_in_month[] = {
	31,28,31,
	30,31,30,
	31,31,30,
	31,30,31
};
time_t parse_stdin() {
	static int time_arr[enum_size];
	static time_t last;
	struct tm stm;
	time_t time;
	FILE* log = NULL;
	int i = 0;
	do {
		memset(&stm, 0, sizeof(stm));
		memset(&time, 0, sizeof(time));
		fopen_s(&log, ".timestamp.log", "a");

		printf("年(2000+) 月 日 时 分\n");
		for (i = 0; i < enum_size; i++) {
			ASSERT_RETRY(scanf_s("%d", time_arr + i), "无效数字",1);
		}
		if (i < enum_size){
			system("pause");
			continue;
		}
		stm.tm_year = 100+time_arr[year];
		stm.tm_mon = time_arr[month]-1;
		stm.tm_mday = time_arr[day];
		stm.tm_hour = time_arr[hour];
		stm.tm_min = time_arr[minute];
		time = mktime(&stm);
		// printf("%lld\n", time);
		if (time == -1)
		{
			perror("时间解析失败 请重试\n");
			continue;
		}
		if (time < last)
		{
			perror("这是一个过去的时间点 请重试\n");
			continue;
		}
		last = time;
		if (log)
		{
			fprintf(log, "\n%lld", time);
			for (int i = 0; i < enum_size; i++)
			{
				fprintf(log, "%d ", time_arr[i]);
			}
			fclose(log);
		}
		break;
	} while (1);
	return time;
}
void timeParked(time_t t)
{
	int hour = (int)t / 3600;
	int min = (int)(1.0 * (double)(t % 3600) / 60 + 0.5);
	printf_s("已停（时：分）%02d:%02d\t", hour, min);
	printf_s("收费%d元\n", hour * price_per_hour);
}

void showTimeStamp(time_t t) {
	time_t time_temp = t;
	struct tm* tm = localtime(&time_temp);
	printf("%5d-%02d-%02d %02d:%02d\t",
		1900+tm->tm_year, tm->tm_mon, tm->tm_mday,
		tm->tm_hour, tm->tm_min
	);
	//free(tm);
}